﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace EditoneLib.Serialization
{
    [DataContract(Namespace = "http://editone.app/v1")]
    public class Project : IEditoneSerializable
    {
        public IEditoneSerializer Serializer { set; protected get; }

        [DataMember]
        public IEnumerable<Track> Tracks { get; set; }

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public IDictionary<string, dynamic> Settings { get; set; }

        public EditoneLib.Project DelegatedProject
        {
            get
            {
                var proj = new EditoneLib.Project();
                // Set name
                proj.Name = Name;

                // Import settings
                foreach (var pair in Settings)
                {
                    proj.Settings[pair.Key] = pair.Value;
                }

                //Import tracks
                foreach (var track in Tracks)
                {
                    track.Serializer = Serializer;
                    proj.Add(track.DelegatedTrack);
                }

                return proj;
            }
            set
            {
                Name = value.Name;
                
                Settings = new Dictionary<string, dynamic>((IDictionary<string,dynamic>)value.Settings);

                Tracks = value.Select((track) => new Track() {DelegatedTrack = track, Serializer = Serializer});
            }
        }

    }
}
